<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登陆页面</title>
    <link rel="stylesheet" href="./css/reg.css">
    <link rel="stylesheet" href="./css/common.css">
    <script src="./js/jQuery.min.js"></script>
</head>

<body>
    <!-- 导航栏 -->
    <div class="nav">
        <img src="./img/logo.png" alt="">
        <p>闲聊屋子</p>

    </div>
    <div class="container">
        <p>注册</p>
        <div class="login"><input type="file" id="user_photo"></div>
        <div class="login"><span>用户名</span><input type="text" id="username" placeholder="请输入用户名"></div>
        <div class="login"><span>昵称&nbsp;&nbsp;&nbsp;&nbsp;</span><input type="text" id="nickname" placeholder="请输入昵称">
        </div>
        <div class="login"><span>密码&nbsp;&nbsp;&nbsp;&nbsp;</span>
            <input type="password" id="password" placeholder="请输入密码">
        </div>
        <div class="login"><span>确认密码</span>
            <input type="password" id="conword" placeholder="请输入确认密码">
        </div>
        <div class="login"><span>验证码</span>
            <input type="text" id="vr_code_pre" placeholder="请输入验证码">
            <img src="user/captcha" alt="验证码" id="vr_code" onclick="changeCode()">
        </div>


        <div class="btn">
            <button id="reg" type="submit" onclick="register()">注册</button>
        </div>
    </div>
    <script>
        function register() {
            var $username = jQuery('#username')
            var $password = jQuery('#password')
            var $conword = jQuery('#conword')
            var $vrCode = jQuery('#vr_code_pre')
            var $nickname = jQuery('#nickname')
            if ($username.val() == '') {
                alert('请输入用户名！')
                $username.focus()
                return
            }
            if ($nickname.val() == '') {
                alert('请输入昵称！')
                $nickname.focus()
                return
            }
            if ($password.val() == '') {
                alert('请输入密码！')
                $password.focus()
                return
            }
            if ($conword.val() == '') {
                alert('请输入确认密码！')
                $conword.focus()
                return
            }
            if ($vrCode.val() == '') {
                alert('请输入验证码！')
                $vrCode.focus()
                return
            }
            if ($password.val() != $conword.val()) {
                alert('密码和确认密码不一致！')
                $password.focus()
                return
            }
            jQuery.ajax({
                url: "user/register",
                type: "post",
                data: {
                    "username": $username.val(),
                    "password": $password.val(),
                    "captcha": $vrCode.val(),
                    "nickname": $nickname.val()
                },
                success: function (result) {
                    if (result.code == 200) {
                        alert('注册成功！')
                        if ($('#user_photo').val() == '') {
                            location.href = "./login.html"
                        } else {
                            uploadPhoto()
                        }
                    } else {
                        alert(result.msg)
                    }
                    changeCode()    //更新验证码
                }
            })
        }
        function changeCode() {
            jQuery('#vr_code').attr('src', 'user/captcha?' + Math.random())
        }

        function uploadPhoto() {
            var formData = new FormData();
            formData.append("userPhoto", $("#user_photo")[0].files[0])
            jQuery.ajax({
                url: 'user/up_photo',
                type: 'post',
                data: formData,
                contentType: false,
                processData: false,
                success: function (result) {
                    if (result.code != 200) {
                        alert(result.msg)
                    } else {
                        //头像上传成功
                        location.href = "./login.html"
                    }
                }
            })
        }
    </script>
</body>

</html>